package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ApiAssert;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysLecturer;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.mapper.SysLecturerMapper;
import com.flyme.module.base.provider.service.SysLecturerService;
import com.flyme.module.base.provider.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: zyf
 * @date: 2018/10/24 16:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysLecturerServiceImpl extends BaseServiceImpl<SysLecturerMapper, SysLecturer> implements SysLecturerService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Boolean addLecturer(String content, SysUser sysUser,String summary) {
        //保存user对象
        ApiAssert.notNull("姓名不能为空", sysUser.getUserName());

        sysUserService.save(sysUser);
        //保存lecturer对象
        SysLecturer sysLecturer = new SysLecturer();
        sysLecturer.setId(sysUser.getId());
        sysLecturer.setLecturerImg(sysUser.getAvatar());
        sysLecturer.setLecturerName(sysUser.getUserName());
        sysLecturer.setContent(content);
        sysLecturer.setSummary(summary);
        return save(sysLecturer);
    }

    @Override
    public Boolean updateLecturer(String content, SysUser sysUser,String summary) {
        ApiAssert.notNull("姓名不能为空", sysUser.getUserName());
        sysUserService.updateById(sysUser);
        SysLecturer sysLecturer = new SysLecturer();
        sysLecturer.setId(sysUser.getId());
        sysLecturer.setLecturerImg(sysUser.getAvatar());
        sysLecturer.setLecturerName(sysUser.getUserName());
        sysLecturer.setSummary(summary);
        sysLecturer.setContent(content);
        return updateById(sysLecturer);
    }

    @Override
    public List<SysLecturer> findList(String lecturerName) {
        List<SysLecturer> list = list(q().eq("lecturerName", lecturerName));
        return list;
    }


    @Override
    public Boolean delByIds(Long[] ids) {
        ApiAssert.notNull("参数不能为空", ids);
        sysUserService.delByIds(ids);
        return removeByIds(CollectionUtil.toList(ids));
    }

    /**
     * 首页查询4个讲师
     *
     * @return
     */
    public List<EntityMap> selectLecturers(PagerInfo pagerInfo) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("l.id,user.userName lecturerName,user.avatar lecturerImg,l.content,summary");
        cq.eq("l.isShow", CommonConstants.ENABLED);
        cq.createAlias("user", "user");
        List<EntityMap> list = (List<EntityMap>) pageList(cq).getRows();
        for (EntityMap lecturer : list) {
            lecturer.put("contentText", "");
            if (ObjectUtils.isNotEmpty(lecturer.get("content"))) {
                String content = lecturer.get("content").toString();
                lecturer.put("contentText", content.replaceAll("\\<.*?>", "").replaceAll("&nbsp;", ""));
            }
        }
        return list;
    }


    /**
     * 网站查询推荐讲师
     *
     * @return
     */
    public PqGrid selectIndexLecturersWeb(PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("isShow", CommonConstants.ENABLED);
        queryWrapper.orderByAsc("rand()");
        return pageList(queryWrapper);
    }

    /**
     * 网站查询推荐个讲师
     * @return
     */
    public PqGrid selectLecturersWeb(PagerInfo pagerInfo){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("isShow", CommonConstants.ENABLED);
        return pageList(queryWrapper);
    }

    /**
     * 搜索查询导师
     */
    public List<EntityMap> searchLecturer(String keyWord, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("id,lecturerName,lecturerImg,content,age,summary");
        queryWrapper.like("lecturerName", keyWord);
        queryWrapper.eq("isShow", CommonConstants.ENABLED);
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }

    /**
     * 网站搜索查询导师
     */
    public PqGrid searchLecturerWeb(String keyWord, PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("id,lecturerName,lecturerImg,content,age,summary");
        queryWrapper.like("lecturerName", keyWord);
        queryWrapper.eq("isShow", CommonConstants.ENABLED);
        PqGrid pqGrid = pageList(queryWrapper);
        return pqGrid;
    }

}
